﻿using Common.Contract.Authentication.Record;
using Common.Contract.CrossSelling.Record;
using Common.Contract.CrossSelling.Request;
using Common.Contract.CrossSelling.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveProductXSellingByCifServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveProductXSellingByCifRequest, RetrieveProductXSellingByCifResponse>
    {
        #region Properties
        public UserRecord Dao;
        #endregion
        public RetrieveProductXSellingByCifServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveProductXSellingByCifRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }

            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
        }

        protected override bool OnExecute(RetrieveProductXSellingByCifResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            CustomerCrossellingProductDetailRecord record = new CustomerCrossellingProductDetailRecord();
            Dictionary<int, CrossSellingCustomer> listXSellingPerCif = Providers.CrossSellingProducts.RetrieveCrossSellingByCif(Request.Cif).ToDictionary(x => x.ProductId);
            Dictionary<string, CustUsedProductInPast> listCustUsedProduct = Providers.CrossSellingProducts.RetrieveCustUsedProductInPastByCif(Request.Cif).ToDictionary(x => x.Product);

            List<CrossSellingProduct> listProductItem = Providers.CrossSellingProducts.RetrieveListProductCrossSelling();
            List<ProductSMEByProductHolding> listProductSME = Providers.CrossSellingProducts.RetrieveListHoldProductCrossSelling();
            record.listUsingProduct = listProductItem.Where(x => listXSellingPerCif.ContainsKey(x.Id)).Select(x =>
                new XSellingProductRecord
                {
                    Id = x.Id,
                    ProductName = Request.Lang.Equals("en") ? x.ProductNameEn : x.ProductName,
                    CustomerLift = listXSellingPerCif[x.Id].LiftIndex.ToString()
                }).ToList();
            record.listUsedProduct = listProductSME.Where(x => listCustUsedProduct.ContainsKey(x.Product)).Select(x =>
                new XSellingProductRecord
                {
                    Product = x.Product,
                    ProductName = Request.Lang.Equals("en") ? x.ProductNameEn : x.ProductName,
                    LastUse = listCustUsedProduct[x.Product].Yearmonth.ToString()
                }).ToList();
            record.listPreApproved = new List<XSellingProductRecord>();
            response.Record = record;
            return isSuccess;
        }
    }

}
